/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.mailbox.ContactConstants;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.index.SortBy;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.Contact.Attachment;
/*     */ import com.zimbra.cs.mailbox.ContactGroup;
/*     */ import com.zimbra.cs.mailbox.ContactGroup.Member;
/*     */ import com.zimbra.cs.mailbox.ContactGroup.Member.Type;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import java.io.BufferedOutputStream;
/*     */ import java.io.IOException;
/*     */ import java.io.OutputStream;
/*     */ import java.util.Iterator;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class ContactFolderFormatter
/*     */   extends Formatter
/*     */ {
/*     */   private static final byte FIELD_DELIMITER = 29;
/*     */   private static final byte CONTACT_DELIMITER = 30;
/*     */   private static final String CONTENT_TYPE = "text/x-zimbra-delimitted-fields";
/*     */   
/*     */   private static enum Delimiter
/*     */   {
/*  48 */     Field,  Contact;
/*     */     
/*     */     private Delimiter() {}
/*     */   }
/*     */   
/*     */   public void formatCallback(UserServletContext context) throws UserServletException, ServiceException, IOException, ServletException {
/*  54 */     if (!(context.target instanceof Folder)) {
/*  55 */       throw UserServletException.notImplemented("can only handle Folders");
/*     */     }
/*  57 */     Folder f = (Folder)context.target;
/*  58 */     if (f.getDefaultView() != MailItem.Type.CONTACT) {
/*  59 */       throw UserServletException.notImplemented("can only handle Contact Folders");
/*     */     }
/*  61 */     String v = (String)context.params.get("t");
/*  62 */     Delimiter d = Delimiter.Field;
/*  63 */     if ((v != null) && (v.equals("2")))
/*  64 */       d = Delimiter.Contact;
/*  65 */     v = (String)context.params.get("all");
/*  66 */     boolean allContacts = false;
/*  67 */     if (v != null) {
/*  68 */       allContacts = true;
/*     */     }
/*  70 */     ItemIdFormatter ifmt = new ItemIdFormatter(context.getAuthAccount(), context.targetAccount, false);
/*  71 */     context.resp.setContentType("text/x-zimbra-delimitted-fields");
/*  72 */     OutputStream out = new BufferedOutputStream(context.resp.getOutputStream());
/*     */     
/*  74 */     Iterator<? extends MailItem> contacts = null;
/*  75 */     contacts = getMailItems(context, 0L, 0L, 0L);
/*  76 */     while (contacts.hasNext())
/*  77 */       printContact((MailItem)contacts.next(), out, ifmt, d);
/*  78 */     if (allContacts) {
/*  79 */       for (Folder folder : context.targetMailbox.getFolderList(context.opContext, SortBy.NONE))
/*     */       {
/*  81 */         if ((folder != context.target) && (folder.getType() != MailItem.Type.MOUNTPOINT) && (folder.getDefaultView() == MailItem.Type.CONTACT))
/*     */         {
/*     */ 
/*     */ 
/*  85 */           for (MailItem item : getMailItemsFromFolder(context, folder, 0L, 0L, 0L))
/*  86 */             printContact(item, out, ifmt, d);
/*     */         }
/*     */       }
/*     */     }
/*  90 */     out.flush();
/*     */   }
/*     */   
/*     */   private void printContact(MailItem item, OutputStream out, ItemIdFormatter ifmt, Delimiter d) throws IOException {
/*  94 */     if (!(item instanceof Contact)) {
/*  95 */       return;
/*     */     }
/*     */     
/*  98 */     out.write("id".getBytes("UTF-8"));
/*  99 */     out.write(29);
/* 100 */     out.write(ifmt.formatItemId(item).getBytes("UTF-8"));
/* 101 */     out.write(29);
/*     */     
/* 103 */     out.write("l".getBytes("UTF-8"));
/* 104 */     out.write(29);
/* 105 */     out.write(ifmt.formatItemId(item.getFolderId()).getBytes("UTF-8"));
/* 106 */     out.write(29);
/*     */     
/* 108 */     out.write("d".getBytes("UTF-8"));
/* 109 */     out.write(29);
/* 110 */     out.write(Long.toString(item.getDate()).getBytes("UTF-8"));
/* 111 */     out.write(29);
/*     */     
/* 113 */     out.write("rev".getBytes("UTF-8"));
/* 114 */     out.write(29);
/* 115 */     out.write(Integer.toString(item.getSavedSequence()).getBytes("UTF-8"));
/* 116 */     out.write(29);
/*     */     try
/*     */     {
/* 119 */       String fileAsStr = ((Contact)item).getFileAsString();
/* 120 */       out.write("fileAsStr".getBytes("UTF-8"));
/* 121 */       out.write(29);
/* 122 */       out.write(fileAsStr.getBytes("UTF-8"));
/*     */     }
/*     */     catch (ServiceException se) {}
/*     */     
/* 126 */     Map<String, String> fields = ((Contact)item).getFields();
/* 127 */     for (String k : fields.keySet()) {
/* 128 */       if (ContactConstants.A_groupMember.equals(k)) {
/* 129 */         printContactGroup((String)fields.get(k), out);
/*     */       } else {
/* 131 */         out.write(29);
/* 132 */         out.write(k.getBytes("UTF-8"));
/* 133 */         out.write(29);
/* 134 */         out.write(((String)fields.get(k)).getBytes("UTF-8"));
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 139 */     List<Contact.Attachment> attachments = ((Contact)item).getAttachments();
/* 140 */     for (Contact.Attachment attachment : attachments) {
/* 141 */       if (attachment.getName().equals(ContactConstants.A_image)) {
/* 142 */         out.write(29);
/* 143 */         out.write((ContactConstants.A_image + "part").getBytes("UTF-8"));
/* 144 */         out.write(29);
/* 145 */         out.write(attachment.getPartName().getBytes("UTF-8"));
/* 146 */         break;
/*     */       }
/*     */     }
/*     */     
/* 150 */     switch (d) {
/*     */     case Field: 
/* 152 */       out.write(29);
/* 153 */       break;
/*     */     case Contact: 
/* 155 */       out.write(30);
/*     */     }
/*     */   }
/*     */   
/*     */   private void printContactGroup(String encodedContactGroup, OutputStream out) throws IOException
/*     */   {
/* 161 */     ContactGroup contactGroup = null;
/*     */     try
/*     */     {
/* 164 */       contactGroup = ContactGroup.init(encodedContactGroup);
/*     */     } catch (ServiceException e) {
/* 166 */       ZimbraLog.contact.warn("unable to init contact group", e);
/*     */     }
/*     */     
/* 169 */     if (contactGroup == null) {
/* 170 */       return;
/*     */     }
/*     */     
/* 173 */     for (ContactGroup.Member member : contactGroup.getMembers()) {
/* 174 */       ContactGroup.Member.Type type = member.getType();
/* 175 */       out.write(29);
/* 176 */       out.write(type.getDelimittedFieldsEncoded().getBytes("UTF-8"));
/* 177 */       out.write(29);
/* 178 */       out.write(member.getValue().getBytes("UTF-8"));
/*     */     }
/*     */   }
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/* 184 */     return FormatterFactory.FormatType.CONTACT_FOLDER;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/ContactFolderFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */